﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Monopoly
{
    public partial class Setup : Form
    {
        private List<Color> defaultColors = new List<Color>() { Color.Red, Color.Blue, Color.Green, Color.Yellow, Color.Orange, Color.Purple };
        private List<Player> players = new List<Player>();

        public Setup()
        {
            InitializeComponent();
        }

        private void cbPlayer_SelectedIndexChanged(object sender, EventArgs e)
        {
            Player p = players[cbPlayer.SelectedIndex];
            tbName.Text = p.Name;
        }

        private void btnColor_Click(object sender, EventArgs e)
        {
            colorDialog = new ColorDialog();
            if (colorDialog.ShowDialog() == DialogResult.OK)
            {
                btnColor.BackColor = colorDialog.Color;
            }
        }

        private void cbPlayers_SelectedIndexChanged(object sender, EventArgs e)
        {
            // empty combobox and list
            cbPlayer.Items.Clear();
            players.Clear();
            Player p;
            // add new players to combobox and list
            for (int i = 1; i <= cbPlayers.SelectedIndex + 2; i++)
            {
                players.Add(new Player("Player " + i, defaultColors[i]));
                cbPlayer.Items.Add("Player" + i);
                cbstartplayer.Items.Add("Player" + i);

            }
            cbPlayer.SelectedIndex = 0;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            check_int();
            check_empty();
            for (int j = 0; j < players.Count; j++)
            {
                players[j].One = int.Parse(txtone.Text);
                players[j].Five = int.Parse(txtfive.Text);
                players[j].Ten = int.Parse(txtten.Text);
                players[j].Twenty = int.Parse(txttwenty.Text);
                players[j].Fifty = int.Parse(txtfifty.Text);
                players[j].Hunderd = int.Parse(txthunderd.Text);
                players[j].Fivehunderd = int.Parse(txtfivehunderd.Text);

            }
            if (cbdobbel.Checked)
	        {
               new throw_start(players).Show();
	        }
               // new Form1(players).Show();
            
            
        }

        private void tbName_TextChanged(object sender, EventArgs e)
        {
            players[cbPlayer.SelectedIndex].Name = tbName.Text;
            cbPlayer.Items[cbPlayer.SelectedIndex] = tbName.Text;
        }

        private void btnColor_BackColorChanged(object sender, EventArgs e)
        {
            players[cbPlayer.SelectedIndex].Color = btnColor.BackColor;
        }

        private void cbnormaal_CheckedChanged(object sender, EventArgs e)
        {
            txtfifty.Clear(); txtfive.Clear(); txtfivehunderd.Clear(); txthunderd.Clear(); txtone.Clear(); txtten.Clear(); txttwenty.Clear();
            if (cbnormaal.Checked)
            {
                cbalternatief.Checked = false;
                txtone.Text = "" + 5;
                txtfive.Text = "" + 7;
                txtten.Text = "" + 10;
                txttwenty.Text = "" + 3;
                txtfifty.Text = "" + 4;
                txthunderd.Text = "" + 6;
                txtfivehunderd.Text = "" + 1;
            }

        }

        private void cbalternatief_CheckedChanged(object sender, EventArgs e)
        {
            if (cbalternatief.Checked)
            {
                cbnormaal.Checked = false;
                check_empty();

            }
            else
            {
                txtfifty.Clear(); txtfive.Clear(); txtfivehunderd.Clear(); txthunderd.Clear(); txtone.Clear(); txtten.Clear(); txttwenty.Clear();
            }
        }

        private void check_empty()
        {
            if ((txtone.Text == "") || (Convert.ToInt32(txtone.Text) < 0))
            {
                txtone.Text = "" + 0;
            }
            if ((txtfive.Text == "") || (Convert.ToInt32(txtfive.Text) < 0))
            {
                txtfive.Text = "" + 0;
            }
            if ((txtten.Text == "") || (Convert.ToInt32(txtten.Text) < 0))
            {
                txtten.Text = "" + 0;
            }
            if ((txttwenty.Text == "") || (Convert.ToInt32(txttwenty.Text) < 0))
            {
                txttwenty.Text = "" + 0;
            }
            if ((txtfifty.Text == "") || (Convert.ToInt32(txtfifty.Text) < 0))
            {
                txtfifty.Text = "" + 0;
            }
            if ((txthunderd.Text == "") || (Convert.ToInt32(txthunderd.Text) < 0))
            {
                txthunderd.Text = "" + 0;
            }
            if ((txtfivehunderd.Text == "") || (Convert.ToInt32(txtfivehunderd.Text) < 0))
            {
                txtfivehunderd.Text = "" + 0;
            }
        }

        private void check_int()
        {
            if (!IsInt(txtone.Text))
            {
                txtone.Text = "" + 0;
            }
            if (!IsInt(txtfive.Text))
            {
                txtfive.Text = "" + 0;
            }
            if (!IsInt(txtten.Text))
            {
                txtten.Text = "" + 0;
            }
            if (!IsInt(txttwenty.Text))
            {
                txttwenty.Text = "" + 0;
            }
            if (!IsInt(txtfifty.Text))
            {
                txtfifty.Text = "" + 0;
            }
            if (!IsInt(txthunderd.Text))
            {
                txthunderd.Text = "" + 0;
            }
            if (!IsInt(txtfivehunderd.Text))
            {
                txtfivehunderd.Text = "" + 0;
            }
        }

        private bool IsInt(string value)
        {
            try
            {
                Convert.ToInt32(value);
                return true;
            }
            catch
            {
                return false;
            }
        }

        private void cbstartplayer_SelectedIndexChanged(object sender, EventArgs e)
        {
            players[cbstartplayer.SelectedIndex].Turn = true;
        }

        private void cbkies_CheckedChanged(object sender, EventArgs e)
        {
            if (cbkies.Checked)
            {
                cbstartplayer.Visible = true;
                lblstart_speler.Visible = true;
            }
            else
            {
                cbstartplayer.Visible = false;
                lblstart_speler.Visible = false;
            }

        }

    }
}
